package com.kingsalert.kingsalert.activity.ln;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.base.BaseActivity;
import com.kingsalert.kingsalert.bean.userinfo.LnAplyDetailInfo;
import com.kingsalert.kingsalert.bean.userinfo.LnAplyItem;
import com.kingsalert.kingsalert.dialog.CustomerServiceDialog;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.util.ArrayList;

public class LnAplyActivity extends BaseActivity {

    private TextView currencyTopTv,amountTopTv,termTopTv,termUnitTopTv;
    private LinearLayout pGroupLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ln_aply);
        page = "APPLY";

        SwipeRefreshLayout refreshLayout = findViewById(R.id.ala_refreshview);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpUtil.getInstance().requestLnAplyDetail(this,this::onAplyDetailResp);
        });

        findViewById(R.id.ala_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.ala_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog(this).show());

        currencyTopTv = findViewById(R.id.ala_tv_currencytop);
        amountTopTv = findViewById(R.id.ala_tv_amounttop);
        termTopTv = findViewById(R.id.ala_tv_termtop);
        termUnitTopTv = findViewById(R.id.ala_tv_termunittop);
        pGroupLayout = findViewById(R.id.ala_layout_param_group);


        findViewById(R.id.ala_btn_submit).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page ,"SUBMIT_CLICK","");
            HttpUtil.getInstance().aply(this,this::onLnAplyResp);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUtil.getInstance().requestLnAplyDetail(this,this::onAplyDetailResp);
    }

    @SuppressLint("SetTextI18n")
    public void onAplyDetailResp(){

        String amount = LnAplyDetailInfo.getInstance().getAmount();
        String currency = LnAplyDetailInfo.getInstance().getCurrency();

        amountTopTv.setText(amount);
        currencyTopTv.setText(currency);
        termTopTv.setText(LnAplyDetailInfo.getInstance().getDays());
        termUnitTopTv.setText(LnAplyDetailInfo.getInstance().getTermUnit(this));

        refreshParamView();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView(){

        pGroupLayout.removeAllViews();

        ArrayList<LnAplyItem> params = LnAplyDetailInfo.getInstance().getDetailItemList();

        for (int i=0;i<params.size();i++){

            LnAplyItem param = params.get(i);

            if (param == null) continue;

            View view;
            TextView pNameTv;
            TextView pValueTv;

            if (param.isTitle()){
                if (i>0) pGroupLayout.addView(LayoutInflater.from(this).inflate(R.layout.item_lnaply_divider,null));
                view = LayoutInflater.from(this).inflate(R.layout.item_lnaply_title,null);
                pNameTv = view.findViewById(R.id.ilt_tv_p_name);
                pValueTv = view.findViewById(R.id.ilt_tv_p_value);
            }else {
                view = LayoutInflater.from(this).inflate(R.layout.item_lnaply_detail,null);
                pNameTv = view.findViewById(R.id.ild_tv_p_name);
                pValueTv = view.findViewById(R.id.ild_tv_p_value);
            }

            pNameTv.setText(param.getShowName());
            pValueTv.setText(param.getShowDetail());

            pGroupLayout.addView(view);

        }

    }

    public void onLnAplyResp(){
        HttpUtil.getInstance().userProcess(this,this::onProcessResp);
    }

    public void onProcessResp(){
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

}